package org.example.chapter7.main;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Repository;

/**
 * @author CoderXshuai
 * @date 2020/7/29/0029 19:47
 */
@SpringBootApplication(scanBasePackages = "org.example.chapter7")
@MapperScan(basePackages = "org.example.chapter7", annotationClass = Repository.class)
@EnableCaching
public class Chapter7Application {

    //注入RedisTemplate
    @Autowired
    private RedisTemplate redisTemplate;

    //Redis连接工厂
    @Qualifier("RedisConnectionFactory")
    @Autowired
    private RedisConnectionFactory connectionFactory;

//    //Redis消息监听器
//    @Autowired
//    private MessageListener redisMsgListener;
//
//    //任务池
//    private ThreadPoolTaskScheduler taskScheduler;

    //    public static void main(String[] args) {
//        ApplicationContext ctx = new AnnotationConfigApplicationContext(RedisConfig.class);
//        RedisTemplate redisTemplate = ctx.getBean(RedisTemplate.class);
////        redisTemplate.opsForValue().set("key1", "balg");
////        redisTemplate.opsForHash().put("hash", "field", "hvalu5e");
////        System.out.println(redisTemplate.opsForHash().get("hash", "field"));
////        redisTemplate.execute(new SessionCallback() {
////            @Override
////            public Object execute(RedisOperations redisOperations) throws DataAccessException {
////                redisOperations.opsForValue().set("key1", "SeesioncallbackValue");
////                redisOperations.opsForHash().put("hash", "field", "Seesioncallback");
////                System.out.println(redisOperations.opsForValue().get("key1"));
////                return null;
////            }
////        });
////        redisTemplate.execute((SessionCallback)  operations -> {
////            operations.opsForValue().set("ke1","b");
////            operations.opsForValue().set("key1", "SeesioncallbackValue");
////            operations.opsForHash().put("hash", "field", "Seesioncallback");
////            return null;
////        });
//    }
    public static void main(String[] args) {
        SpringApplication.run(Chapter7Application.class);
    }

//    /**
//     * 创建任务池,运行线程等待处理Redis的消息
//     *
//     * @return
//     */
//    @Bean
//    public ThreadPoolTaskScheduler initTaskScheduler() {
//        if (taskScheduler != null) {
//            return taskScheduler;
//        }
//        taskScheduler = new ThreadPoolTaskScheduler();
//        taskScheduler.setPoolSize(20);
//        return taskScheduler;
//    }
//
//    @Bean
//    public RedisMessageListenerContainer initRedisContainer() {
//        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
//        //Redis连接工厂
//        container.setConnectionFactory(connectionFactory);
//        //设置运行任务池
//        container.setTaskExecutor(initTaskScheduler());
//        //定义监听渠道,名称为topic1
//        Topic topic = new ChannelTopic("topic1");
//        //使用监听器监听Redis的消息
//        container.addMessageListener(redisMsgListener, topic);
//        return container;
//    }

//    //定义自定义后初始化方法
//    @PostConstruct
//    public void init() {
//        initRedisTemplate();
//        System.out.println("Chapter7Application.init");
//    }

    //设置RedisTemplate

    /**
     * 改变RedisTemplate对于键的序列化策略
     */
//    private void initRedisTemplate() {
//        RedisSerializer stringSerializer = redisTemplate.getStringSerializer();
//        redisTemplate.setKeySerializer(stringSerializer);
//        redisTemplate.setHashKeySerializer(stringSerializer);
//    }

}
